package com.runa.monitor.platinterface.backwork.util;

import java.io.*;

public class FileUtils {



    public static BufferedReader getBufferedReader(String url) {
        FileReader fr= null;
        BufferedReader br= null;
        try {
            fr = new FileReader(url);
            br = new BufferedReader(fr);
            return br;
//            String line="";
//            String[] arrs=null;
//            while ((line=br.readLine())!=null) {
//                arrs=line.split("\"");
//            }
        } catch (IOException e) {
            
        }
        return null;
    }

    public static BufferedWriter getBufferedWriter(String url) {
        FileWriter fw= null;
        BufferedWriter bw= null;
        try {
            fw = new FileWriter(new File(url));
            bw = new BufferedWriter(fw);
            return bw;
            //
        } catch (IOException e) {
            
        }
        return null;
    }

    public static void closeBufferedReader(BufferedReader br){
        if (br!=null){
            try {
                br.close();//这里也关了FileReader
            } catch (IOException e) {
                
            }
        }
    }
    public static void closeBufferedWriter(BufferedWriter bw){
        if (bw!=null){
            try {
                bw.close();//这里也关了FileWriter
            } catch (IOException e) {
                
            }
        }
    }
    public static void appendAndNewLine(String file, String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(conent);
            out.newLine();
        } catch (Exception e) {
            
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                
            }
        }
    }


    public static void method2(String fileName, String content) {
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            
        }
    }


    public static void method3(String fileName, String content) {
        try {
            // 打开一个随机访问文件流，按读写方式
            RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
            // 文件长度，字节数
            long fileLength = randomFile.length();
            // 将写文件指针移到文件尾。
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
            randomFile.close();
        } catch (IOException e) {
            
        }
    }
}
